import 'package:flutter/material.dart';
import './ProductCard.dart';
import '../../ScopedModels/ScopedProduct.dart';
import '../../Models/product.dart';
import 'package:scoped_model/scoped_model.dart';


class Products extends StatelessWidget {

 //自动赋值 每次替换_products属性而不是改变
  @override
  Widget build(BuildContext context) {
    print('[Products Widget] build');
    // TODO: implement build
    return ScopedModelDescendant<ScopedProduct>(
      //数据改变时调用 
        builder: (BuildContext context, Widget child, ScopedProduct model) {
      return _buildProductList(model.disPlayProducts);
    });
  }

  Widget _buildProductList(List<Product> products) {
    Widget productList;
    if (products.length > 0) {
      productList = ListView.builder(
        itemBuilder: (BuildContext context, int index) =>
            ProductCard(products[index],index),
        itemCount: products.length,
      );
    } else {
      productList = Container();
    }
    return productList;
  }
}
